import {
  login,
  getInfo,
  getInfotwo,
  logout
} from '@/api/user'
import {
  getToken,
  setToken,
  removeToken
} from '@/utils/auth'
import {
  resetRouter
} from '@/router'

import {
  constantRoutes
} from '../../router/index'


const routeInfo = JSON.parse(localStorage.getItem("info"));
console.log(routeInfo);
const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    init: false,
    route: routeInfo, // 存储一级路由
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SER_ROUTE: (state, route) => {
    console.log(route);
    state.route = route
  },
  set_init: (state, status) => {
    state.init = status //是permission.js中的初始化判断
    // 注意data中要引入
  }
}
const aaa = localStorage.getItem("info");

const actions = {
  // user login
  login({
    commit
  }, userInfo) {
    const {
      username,
      password
    } = userInfo
    return new Promise((resolve, reject) => {
      login({
        UserId: username.trim(),
        pw: password
      }).then(response => {
        const data = response
        // console.log(data);
        commit('SET_TOKEN', data.ResList.Id)
        setToken(data.ResList.Id)
        resolve()
      }).catch(error => {
        reject(error)
        // }).then((_)=>{
        //   console.log(11);
        //   getInfo().then(response =>{
        //     const res = response;
        //     console.log(res);
        //   })
      })
    })
  },

  setRouterList({
    commit
  }, route) {
    commit('SER_ROUTE', constantRoutes.concat(route))
  },

  // get user info
  // getInfo({ commit, state }) {
  //   return new Promise((resolve, reject) => {
  //     getInfo(state.token).then(response => {
  //       const {
  //         data
  //       } = response

  //       if (!data) {
  //         return reject('Verification failed, please Login again.')
  //       }

  //       commit('SET_ROLES', data.ResList)

  //       // const {
  //       //   name,
  //       //   avatar
  //       // } = data

  //       // commit('SET_NAME', name)
  //       // commit('SET_AVATAR', avatar)

  //       resolve(data)
  //     }).catch(error => {
  //       reject(error)
  //     })
  //   })
  // },

  // 获取用户权限信息
  getInfo({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const {
          data
        } = response
        // console.log(data)
        if (!data) {
          reject('验证失败，请重新登录.')
        }

        let roles = []
        roles = roles.concat(data.ResList)


        // // 角色必须是非空数组
        if (!roles || roles.length <= 0) {
          reject('getInfo: 必须是非空数组!')
        }
        commit('SET_ROLES', roles)
        const users = {
          roles: roles,
        }
        // console.log(users)
        resolve(users)
      }).catch(error => {
        reject(error)
      })
    })
  },


  // user logout
  logout({
    commit,
    state
  }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('set_init', false) //重置初始化
        commit('RESET_STATE', '')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({
    commit
  }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE', '')
      resolve()
    })
  },

  getroute({
    commit
  }) {
    const {} = data;
    return new Promise((res, err) => {
      getInfo().then(res => {
        console.log(res);
      }).catch(err => {
        console.log(err);
      }).then(res => {
        console.log(res.ResList);
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
